package com.isdepci.project.factor.service.impl;

import java.util.List;

import com.github.pagehelper.util.StringUtil;
import com.isdepci.common.constant.UserConstants;
import com.isdepci.common.utils.DateUtils;
import com.isdepci.common.utils.StringUtils;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.factor.domain.CategorizeInfo;
import com.isdepci.project.factor.utils.FactorRedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.isdepci.project.factor.mapper.StandardStageMapper;
import com.isdepci.project.factor.domain.StandardStage;
import com.isdepci.project.factor.service.IStandardStageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目标准阶段管理Service业务层处理
 *
 * @author isdepci
 * @date 2024-10-11
 */
@Service
public class StandardStageServiceImpl implements IStandardStageService {

    @Autowired
    private StandardStageMapper standardStageMapper;

    /**
     * 查询项目标准阶段管理
     */
    @Override
    public StandardStage selectStandardStageById(Long id) {
        return standardStageMapper.selectStandardStageById(id);
    }

    /**
     * 查询项目标准阶段管理列表
     */
    @Override
    public List<StandardStage> selectStandardStageList(StandardStage standardStage) {
        return standardStageMapper.selectStandardStageList(standardStage);
    }

    /**
     * 新增项目标准阶段管理
     */
    @Override
    public AjaxResult insertStandardStage(StandardStage standardStage) {
        if(StringUtils.isEmpty(standardStage.getName())){
            return AjaxResult.error("名称不能为空！");
        }
        List<StandardStage> list = standardStageMapper.selectListByName(standardStage.getName());
        if (list.size()>0){
            return AjaxResult.error("新增'" + standardStage.getName() + "'失败，名称已存在");
        }
        if(standardStage.getParentId()!=0){
            StandardStage info = standardStageMapper.selectStandardStageById(standardStage.getParentId());
            if (info.getStatus() != 1) {
                return AjaxResult.error("当前分类停用，不允许新增");
            }
            standardStage.setAncestors(info.getAncestors() + "," + standardStage.getParentId());
        }else {
            standardStage.setAncestors("0");
        }
        standardStage.setCreateUser();
        int row = standardStageMapper.insertStandardStage(standardStage);
        if (row > 0) {
            FactorRedisUtils.clearStandardStageCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改项目标准阶段管理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateStandardStage(StandardStage standardStage) {
        if (standardStage.getParentId().equals(standardStage.getId())) {
            return AjaxResult.error("修改'" + standardStage.getName() + "'失败，上级不能是自己");
        }
        StandardStage newParent = standardStageMapper.selectStandardStageById(standardStage.getParentId());
        StandardStage old = standardStageMapper.selectStandardStageById(standardStage.getId());
        if(StringUtils.isNotEmpty(standardStage.getName())&& !old.getName().equals(standardStage.getName())){
            List<StandardStage> list = standardStageMapper.selectListByName(standardStage.getName());
            if (list.size()>0){
                return AjaxResult.error("修改'" + standardStage.getName() + "'失败，名称已存在");
            }
        }
        if (StringUtils.isNotNull(newParent) && StringUtils.isNotNull(old)) {
            String newAncestors = newParent.getAncestors() + "," + newParent.getId();
            String oldAncestors = old.getAncestors();
            standardStage.setAncestors(newAncestors);
            updateChildren(standardStage.getId(), newAncestors, oldAncestors);
        }
        standardStage.setUpdateUser();
        int row = standardStageMapper.updateStandardStage(standardStage);
        if (standardStage.getStatus() == 1) {
            updateParentStatus(standardStage);
        }
        if(old.getStatus()==1&&standardStage.getStatus()!=1){
            standardStageMapper.updateChildrenStatus(standardStage.getId(),2);
        }
        if (row > 0) {
            FactorRedisUtils.clearStandardStageCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    public void updateChildren(Long id, String newAncestors, String oldAncestors) {
        List<StandardStage> children = standardStageMapper.selectChildrenById(id);
        for (StandardStage child : children) {
            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            standardStageMapper.updateChildren(children);
        }
    }

    private void updateParentStatus(StandardStage info){
        String updateBy = info.getUpdateBy();
        info = standardStageMapper.selectStandardStageById(info.getId());
        info.setUpdateBy(updateBy);
        standardStageMapper.updateParentStatus(info);
    }

    /**
     * 删除项目标准阶段管理信息
     */
    @Override
    public AjaxResult deleteStandardStageById(Long id) {
        List<StandardStage> children = standardStageMapper.selectChildrenById(id);
        if (children.size()>0) {
            return AjaxResult.error("存在下级分类,不允许删除");
        }
        int row = standardStageMapper.deleteStandardStageById(id);
        if (row > 0) {
            FactorRedisUtils.clearStandardStageCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
